//
//  KATButtonWindow.m
//  KATFramework
//
//  Created by Kantice on 17/5/19.
//  Copyright (c) 2015年 KatApp. All rights reserved.
//

#import "KATButtonWindow.h"




@interface KATButtonWindow ()

///标题
@property(nonatomic,retain) KATLabel *titleLabel;

///内容
@property(nonatomic,retain) KATLabel *infoLabel;

///分割线
@property(nonatomic,retain) KATShape *split;



@end



@implementation KATButtonWindow


//获取实例（默认样式，只有标题）
+ (instancetype)windowWithTitle:(NSString *)title
{
    return [self windowWithTitle:title titleStyle:nil info:nil infoStyle:nil andButtons:nil];
}


//获取实例（只有标题和样式）
+ (instancetype)windowWithTitle:(NSString *)title andStyle:(KATTextStyle *)style
{
    return [self windowWithTitle:title titleStyle:style info:nil infoStyle:nil andButtons:nil];
}


//获取实例（默认样式，只有提示）
+ (instancetype)windowWithInfo:(NSString *)info
{
    return [self windowWithTitle:nil titleStyle:nil info:info infoStyle:nil andButtons:nil];
}


//获取实例（只有提示和样式）
+ (instancetype)windowWithInfo:(NSString *)info andStyle:(KATTextStyle *)style
{
    return [self windowWithTitle:nil titleStyle:nil info:info infoStyle:style andButtons:nil];
}


//获取实例（默认样式，带标题和提示）
+ (instancetype)windowWithTitle:(NSString *)title andInfo:(NSString *)info
{
    return [self windowWithTitle:title titleStyle:nil info:info infoStyle:nil andButtons:nil];
}


//获取实例（默认样式，带标题和提示和按钮）
+ (instancetype)windowWithTitle:(NSString *)title info:(NSString *)info andButtons:(KATArray<KATButton *> *)buttons
{
    return [self windowWithTitle:title titleStyle:nil info:info infoStyle:nil andButtons:buttons];
}


//获取实例(全参数)
+ (instancetype)windowWithTitle:(NSString *)title titleStyle:(KATTextStyle *)tStyle info:(NSString *)info infoStyle:(KATTextStyle *)iStyle andButtons:(KATArray<KATButton *> *)buttons
{
    KATButtonWindow *window=[[[self alloc] init] autorelease];
    
    [window initData];
    
    window.isTapShadowToHide=NO;
    window.isTapContentToHide=NO;
    window.isBlurStyle=YES;
    window.showMode=KATPopWindowAnimationTypeBlurScale;
    window.hideMode=KATPopWindowAnimationTypeAlpha;
    
    [window initWindow];
    
    
    //样式
    window.contentWidth=sqrt(MIN(SCREEN_MIN,500.0))*15.0;
    window.contentPadding=sqrt(MIN(SCREEN_MIN,500.0))*0.8;
    window.buttonHeight=sqrt(MIN(SCREEN_MIN,500.0))*1.6;
    window.lineWidth=0.5;
    window.cornerRadius=10.0;
    window.lineColor=KCOLOR_LINE;

    //默认的样式
    if(!tStyle)
    {
        tStyle=[KATTextStyle textStyle];
        
        tStyle.textFont=[UIFont boldSystemFontOfSize:window.contentPadding*1.16];
        tStyle.textColor=KCOLOR_NIGHT;
        tStyle.hasDateStyle=NO;
        tStyle.textAlignment=NSTextAlignmentCenter;
        tStyle.marginTop=1;
        tStyle.marginLeft=1;
        tStyle.marginRight=1;
        tStyle.marginBottom=1;
    }
    
    if(!iStyle)
    {
        iStyle=[KATTextStyle textStyle];
        
        iStyle.textFont=[UIFont systemFontOfSize:window.contentPadding*1.04];
        iStyle.textColor=KCOLOR_DARK;
        iStyle.hasDateStyle=NO;
        iStyle.textAlignment=NSTextAlignmentCenter;
        iStyle.marginTop=1;
        iStyle.marginLeft=1;
        iStyle.marginRight=1;
        iStyle.marginBottom=1;
    }
    
    window.titleLabel=[KATLabel labelWithFrame:CGRectZero andStyle:nil];
    window.infoLabel=[KATLabel labelWithFrame:CGRectZero andStyle:nil];
    
    [window.content addSubview:window.titleLabel];
    [window.content addSubview:window.infoLabel];
    
    //水平分割线
    window.split=[KATShape shape];

    [window.content.layer addSublayer:window.split];
    
    window.buttons=buttons;
    
    window.title=title;
    window.titleStyle=tStyle;
    window.info=info;
    window.infoStyle=iStyle;
    
    [window updateWindow];
    
    return window;
}


- (void)setTitle:(NSString *)title
{
    if(title!=_title)
    {
        [_title release];
        _title=[title copy];
        
        [self updateWindow];
    }
}



- (void)setInfo:(NSString *)info
{
    if(info!=_info)
    {
        [_info release];
        _info=[info copy];
        
        [self updateWindow];
    }
}


- (void)setTitleStyle:(KATTextStyle *)titleStyle
{
    if(titleStyle!=_titleStyle)
    {
        [_titleStyle release];
        _titleStyle=[titleStyle retain];
        
        [self updateWindow];
    }
}


- (void)setInfoStyle:(KATTextStyle *)infoStyle
{
    if(infoStyle!=_infoStyle)
    {
        [_infoStyle release];
        _infoStyle=[infoStyle retain];
        
        [self updateWindow];
    }
}

- (void)setContentWidth:(float)contentWidth
{
    _contentWidth=contentWidth;
    
    [self updateWindow];
}


- (void)setContentPadding:(float)contentPadding
{
    _contentPadding=contentPadding;
    
    [self updateWindow];
}


- (void)setButtonHeight:(float)buttonHeight
{
    _buttonHeight=buttonHeight;
    
    [self updateWindow];
}


- (void)show
{
    [self updateWindow];
    
    [super show];
}


//设置按钮
- (void)setButtons:(KATArray<KATButton *> *)buttons
{
    //空按钮修正
    if(!buttons)
    {
        buttons=[KATArray arrayWithCapacity:1];
    }
    
    //默认的按钮
    if(buttons.length<1)
    {
        [buttons put:[KATButton buttonWithFrame:CGRectZero type:KATButtonTypeOK color:KCOLOR_BLUE lineWidth:1.5 scale:1.0 andOnClickAction:^
        {
            [self hide];
        }]];
    }
    
    if(buttons!=_buttons)
    {
        //清除原有的控件
        for(int i=0;i<_buttons.length;i++)
        {
            [_buttons[i] removeFromSuperview];
        }
        
        [_buttons release];
        _buttons=[buttons retain];
        
        //加入视图
        for(int i=0;i<_buttons.length;i++)
        {
            [self.content addSubview:_buttons[i]];
        }
        
        [self updateWindow];
    }

}


//更新窗口
- (void)updateWindow
{
    _titleLabel.frame=CGRectMake(_contentPadding, _contentPadding, _contentWidth-2*_contentPadding, 0);
    
    _titleLabel.text=_title;
    
    if(_title)
    {
        _titleLabel.style=_titleStyle;
        [_titleLabel adjustFrame];
    }
    
    _infoLabel.frame=CGRectMake(_contentPadding, _titleLabel.frame.origin.y+_titleLabel.frame.size.height, _contentWidth-2*_contentPadding, 0);
    
    _infoLabel.text=_info;
    
    if(_info)
    {
        _infoLabel.style=_infoStyle;
        [_infoLabel adjustFrame];
    }
    
    //水平分割线
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(_contentWidth, 0)];
    
    _split.path=path.CGPath;
    _split.lineWidth=_lineWidth;
    _split.strokeColor=_lineColor.CGColor;
    _split.bounds=CGRectMake(0, 0, _contentWidth, _lineWidth);
    _split.position=CGPointMake(_contentWidth*0.5, _infoLabel.frame.origin.y+_infoLabel.frame.size.height+_contentPadding);
    
    //按钮
    for(int i=0;i<_buttons.length;i++)
    {
        KATButton *button=_buttons[i];
        
        button.scale=(_buttonHeight-_contentPadding)/_buttonHeight;
        button.frame=CGRectMake((_contentWidth/_buttons.length)*i+((_contentWidth/_buttons.length)-_buttonHeight-_contentPadding)*0.5, _infoLabel.frame.origin.y+_infoLabel.frame.size.height+_contentPadding, _buttonHeight+_contentPadding, _buttonHeight+_contentPadding);
        
        if(_buttons.length>=2 && _buttons.length<=3)//两边调整
        {
            if(i==0)//首个靠左
            {
                button.frame=CGRectMake(0, button.frame.origin.y, button.bounds.size.width, button.bounds.size.height);
            }
            else if(i==_buttons.length-1)//尾个靠右
            {
                button.frame=CGRectMake(_contentWidth-button.bounds.size.width, button.frame.origin.y, button.bounds.size.width, button.bounds.size.height);
            }
        }
    }

    
    self.contentSize=CGSizeMake(_contentWidth, _infoLabel.frame.origin.y+_infoLabel.frame.size.height+_contentPadding+(_contentPadding+_buttonHeight));
}


//调整窗口(方向改变系统通知处理)
- (void)resizeWindow
{
    [super resizeWindow];
}



- (void)dealloc
{    
    [_infoLabel release];
    [_titleLabel release];
    [_title release];
    [_info release];
    [_titleStyle release];
    [_infoStyle release];
    [_lineColor release];
    [_split release];
    [_buttons release];
    
    [super dealloc];
}


@end





